package com.core.dao.jdbcmysql;

import com.core.bean.CarItem;
import com.core.bean.Goods;
import com.core.dao.ICarItemDao;
import com.core.utils.GsonUtils;
import com.core.utils.UUIDTool;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CarItemDao extends JdbcBase implements ICarItemDao {

    @Override
    public int add(CarItem entity) throws Exception {
        String Sql = "INSERT INTO `lct_car_item` VALUES(?,?,?,?,?,?,?) ";
        Connection connection = getConnection();
        PreparedStatement ps = connection.prepareStatement(Sql);
        try {
            ps.setString(1, UUIDTool.getUUID());
            ps.setString(2, entity.getUid());
            ps.setString(3,entity.getShopid());
            ps.setInt(4, entity.getCount());
            ps.setString(5, GsonUtils.toJsonStr(entity.getGoods()));
            ps.setObject(6, entity.getTime());
            ps.setString(7, entity.getRemark());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            ps.close();
            connection.close();
        }
    }

    @Override
    public int remove(String id) throws Exception {
        return 0;
    }

    @Override
    public int modify(CarItem entity) throws Exception {
        String Sql = "update `lct_car_item` set uid = ?,shopid=?,`count`=?,goods=?,`time`=?,remark=? where id = ? ";
        Connection connection = getConnection();
        PreparedStatement ps = connection.prepareStatement(Sql);
        try {
            ps.setString(1,entity.getUid());
            ps.setString(2,entity.getShopid());
            ps.setInt(3, entity.getCount());
            ps.setString(4, GsonUtils.toJsonStr(entity.getGoods()));
            ps.setObject(5, entity.getTime());
            ps.setString(6, entity.getRemark());
            ps.setString(7, UUIDTool.getUUID());
            return ps.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            ps.close();
            connection.close();
        }
    }

    @Override
    public CarItem findEntityById(String id) throws Exception {
        String Sql = "select * from lct_car_item where id = ?";
        Connection connection = getConnection();
        PreparedStatement ps = connection.prepareStatement(Sql);
        CarItem carItem = null;
        try {
            ps.setString(1, id);
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String carid = resultSet.getString("id");
                String uid = resultSet.getString("uid");
                String shopid = resultSet.getString("shopid");
                int count = resultSet.getInt("count");
                Goods goods = GsonUtils.fromJson(resultSet.getString("goods"), Goods.class);
                Date time = resultSet.getDate("time");
                String remark = resultSet.getString("remark");
                carItem = new CarItem(carid,uid,shopid,count,goods,time,remark);
            }
            return carItem;
        } catch (Exception e) {
            throw e;
        } finally {
            ps.close();
            connection.close();
        }
    }

    public List<CarItem> queryList() throws Exception {
        String Sql = "select * from lct_car_item";
        Connection connection = getConnection();
        PreparedStatement ps = connection.prepareStatement(Sql);
        List<CarItem> carItemList = new ArrayList<>();
        try {
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                String carid = resultSet.getString("id");
                String uid = resultSet.getString("uid");
                String shopid = resultSet.getString("shopid");
                int count = resultSet.getInt("count");
                Goods goods = GsonUtils.fromJson(resultSet.getString("goods"), Goods.class);
                Date time = resultSet.getDate("time");
                String remark = resultSet.getString("remark");
                CarItem carItem = new CarItem(carid,uid,shopid,count,goods,time,remark);
                carItemList.add(carItem);
            }
            return carItemList;
        } catch (Exception e) {
            throw e;
        } finally {
            ps.close();
            connection.close();
        }
    }
}
